﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace lchealthy
{
    public partial class frmBusiness : Form
    {
        DataSet ds;
        public frmBusiness()
        {
            InitializeComponent();
        }

        private void btnin_Click(object sender, EventArgs e)
        {
            frmaddbusiness f = new frmaddbusiness();
            f.ShowDialog();
            bind();
        }

        private void frmBusiness_Load(object sender, EventArgs e)
        {
            cmbtype.SelectedIndex = 0;
            bind();

        }

        private void bind()
        {
            string sql = "SELECT businessinfo.id, bname,bdate, businessinfo.borrowtype, ProductInfo.PName, businessinfo.pnum, businessinfo.bstate,businessinfo.pid FROM businessinfo INNER JOIN ProductInfo ON businessinfo.pid = ProductInfo.PID where 1=1";
                if(txtname.Text!="")
                sql+= " and bname like '%"+txtname.Text+"%' ";
            if(cmbtype.SelectedIndex>0)
                sql += " and borrowtype = '" + cmbtype.Text + "' ";
                sql += " and did="+userinfo.id+" order by bstate,bdate desc";
            ds = DbHelperOleDb.Query(sql);
            dgvb.AutoGenerateColumns = false;
            dgvb.DataSource = ds.Tables[0];
            
        }

        private void btnSerach_Click(object sender, EventArgs e)
        {
            bind();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int row = -1;
            row = dgvb.SelectedCells[0].RowIndex;
            if (row != -1)
            {
                int pid = (int)ds.Tables[0].Rows[row]["id"];
                string sql = "update businessinfo set bstate='已处理' where id="+pid;
                DbHelperOleDb.ExecuteSql(sql);
                int spid = (int)ds.Tables[0].Rows[row]["pid"];
                int num = (int)ds.Tables[0].Rows[row]["pnum"];
                string type = ds.Tables[0].Rows[row]["borrowtype"].ToString();
                if (type == "借出")
                {
                    sql = "SELECT pid FROM inventory where Pid=" + spid + " and did=" + userinfo.id;
                    DataSet da = DbHelperOleDb.Query(sql);
                    if (da.Tables[0].Rows.Count == 1)
                    {
                        sql = string.Format("update inventory set [inum] = [inum]+{0} where [Pid] = {1} and did={2}", num, spid,userinfo.id);
                        DbHelperOleDb.ExecuteSql(sql);
                    }
                    else
                    {
                        sql = string.Format("INSERT INTO inventory ([Pid] ,[inum],[did] ) VALUES ({0},{1},{2})", spid, num, userinfo.id);
                        DbHelperOleDb.ExecuteSql(sql);
                    }
                }
                else
                {
                    sql = "update inventory set inum=inum-" + num + " where did=" + userinfo.id + " and pid=" +spid;
                    DbHelperOleDb.ExecuteSql(sql);
                }
                bind();
            }
        }

        private void btnEdite_Click(object sender, EventArgs e)
        {
            int row = -1;
            row = dgvb.SelectedCells[0].RowIndex;
            if (row != -1)
            {
                if (ds.Tables[0].Rows[row]["bstate"].ToString() != "已处理")
                {
                    int pid = (int)ds.Tables[0].Rows[row]["id"];
                    frmaddbusiness f = new frmaddbusiness(pid);
                    f.ShowDialog();
                    bind();
                }
                else
                {
                    MessageBox.Show("本记录已处理完不能再修改了！");
                }
            }

        }

        private void btnOut_Click(object sender, EventArgs e)
        {
            myfun.DataToExcel(dgvb);
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            myprint m = new myprint(dgvb);
            m.Print();
        }
    }
}
